package nme.monetize;


import cpp.Lib;
import hxjson2.JSON;
import nme.monetize.data.InAppPurchaseInvoiceItem;
import nme.monetize.data.InAppPurchaseItem;
import nme.monetize.data.InAppPurchaseReceipt;


class InAppPurchase {
	
	
	public static function getAvailableItems (callbackMethod:Array <InAppPurchaseItem> -> Void):Void {
		
		#if webos
		
		var data:String = iap_get_available_items_json ();
		
		if (data != null) {
			
			Reflect.callMethod (callbackMethod, callbackMethod, [ parseAvailableItemsJSON (data) ] );
			
			return;
			
		}
		
		#end
		
		Reflect.callMethod (callbackMethod, callbackMethod, [ null ] );
		
	}
	
	
	public static function getItemDetails (itemID:String, callbackMethod:InAppPurchaseItem -> Void):Void {
		
		#if webos
		
		var data = iap_get_item_details (itemID);
		
		if (data != null) {
			
			Reflect.callMethod (callbackMethod, callbackMethod, [ parseItemDetailsJSON (data) ] );
			
			return;
			
		}
		
		#end
		
		Reflect.callMethod (callbackMethod, callbackMethod, [ null ] );
		
	}
	
	
	private static function parseAvailableItemsJSON (data:String):Array <InAppPurchaseItem> {
		
		var itemData = JSON.decode (data);
		
		var items = new Array <InAppPurchaseItem> ();
		
		if (itemData.returnValue != null && itemData.returnValue == "true") {
			
			if (itemData.itemInfos != null) {
				
				for (info in cast (itemData.itemInfos, Array <Dynamic>)) {
					
					var item = new InAppPurchaseItem ();
					
					item.itemID = info.itemId;
					item.title = info.title;
					item.description = info.summary;
					item.currency = info.currency;
					item.price = Std.parseFloat (info.price);
					item.status.timesPurchased = Std.parseInt (info.itemStatus.timesPurchased);
					
					if (info.type == "Perishable") {
						
						item.type = InAppPurchaseItemType.PERISHABLE;
						
					} else {
						
						item.type = InAppPurchaseItemType.NON_PERISHABLE;
						
					}
					
					items.push (item);
					
				}
				
			}
			
		}
		
		return items;
		
	}
	
	
	private static function parseItemDetailsJSON (data:String):InAppPurchaseItem {
		
		trace ("Received data: " + data);
		
		var itemData = JSON.decode (data);
		
		var item:InAppPurchaseItem = null;
		
		if (itemData.returnValue != null && itemData.returnValue == "true") {
			
			var info = itemData.itemInfo;
			
			if (info != null) {
				
				item = new InAppPurchaseItem ();
				
				item.itemID = info.itemId;
				item.title = info.title;
				item.description = info.summary;
				item.currency = info.currency;
				item.price = Std.parseFloat (info.price);
				item.status.timesPurchased = Std.parseInt (info.itemStatus.timesPurchased);
				
				if (info.type == "Perishable") {
					
					item.type = InAppPurchaseItemType.PERISHABLE;
					
				} else {
					
					item.type = InAppPurchaseItemType.NON_PERISHABLE;
					
				}
				
				if (info.itemStatus.receipts != null) {
					
					for (receiptData in cast (info.itemStatus.receipts, Array <Dynamic>)) {
						
						var receipt = new InAppPurchaseReceipt ();
						
						receipt.applicationVersion = receiptData.receiptInfo.version;
						receipt.receiptID = receiptData.receiptInfo.receiptId;
						receipt.orderID = receiptData.receiptInfo.orderNo;
						receipt.purchaseTime = receiptData.receiptInfo.purchaseTime;
						
						if (receiptData.receiptInfo.type == "P") {
							
							receipt.type = InAppPurchaseItemType.PERISHABLE;
							
						} else {
							
							receipt.type = InAppPurchaseItemType.NON_PERISHABLE;
							
						}
						
						receipt.applicationID = receiptData.receiptInfo.appId;
						receipt.itemID = receiptData.receiptInfo.itemId;
						receipt.quantity = Std.parseInt (receiptData.receiptInfo.quantity);
						receipt.vendorData = receiptData.receiptInfo.vendorData;
						receipt.accountUniqueID = receiptData.receiptInfo.accountUniqueId;
						receipt.deviceUniqueID = receiptData.receiptInfo.deviceUniqueId;
						
						item.status.receipts.push (receipt);
						
					}
					
				}
				
			}
			
		}
		
		return item;
		
	}
	
	
	public static function purchaseItems (items:Array <InAppPurchaseInvoiceItem>, callbackMethod:InAppPurchaseReceipt -> Void):Void {
		
		//iap_purchase_item (receipt.itemID, receipt.quantity, receipt.vendorData);
		
	}
	
	
	private static var iap_get_available_items_json = Lib.load ("inapp-purchase", "iap_get_available_items_json", 0);
	private static var iap_get_item_details = Lib.load ("inapp-purchase", "iap_get_item_details", 1);
	private static var iap_purchase_item = Lib.load ("inapp-purchase", "iap_purchase_item", 3);
	
	
}